package net.learn.piggy.initializer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import java.util.Properties;

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String port = environment.getProperty("server.port");
        if(port==null){
            Properties properties = new Properties();
            properties.put("server.port", 8888);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("myselfProperties", properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        }
        System.out.println("MyApplicationContextInitializer --> initialize");
    }
}
